package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.IotEnterprise;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.IotEnterpriseDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2020-10-05 16:34:03
 */
public class IotEnterpriseConverts {

    private static Logger LOG = LoggerFactory.getLogger(IotEnterpriseConverts.class);

    public static List<IotEnterpriseDTO> convert(List<IotEnterprise> iotEnterpriseList) {
        if (CollectionUtils.isEmpty(iotEnterpriseList)) {
            return Collections.EMPTY_LIST;
        }

        List<IotEnterpriseDTO> iotEnterpriseListResult = Lists.newArrayList();
        for (IotEnterprise iotEnterprise : iotEnterpriseList) {
            iotEnterpriseListResult.add(convert(iotEnterprise));
        }
        return iotEnterpriseListResult;
    }

    public static IotEnterpriseDTO convert(IotEnterprise iotEnterprise) {
        try {
            return BeanUtils.copy(iotEnterprise, IotEnterpriseDTO.class);
        } catch (Exception e) {
            LOG.error("IotEnterprise到IotEnterpriseDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotEnterprise到IotEnterpriseDTO转换失败", e);
        }
    }

    public static IotEnterprise convert(IotEnterpriseDTO iotEnterpriseDTO) {
        try {
            return BeanUtils.copy(iotEnterpriseDTO, IotEnterprise.class);
        } catch (Exception e) {
            LOG.error("IotEnterpriseDTO到IotEnterprise转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotEnterpriseDTO到IotEnterprise转换失败", e);
        }
    }
}